{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "89bbc4d0",
   "metadata": {},
   "source": [
    "### Extract Data From Calls & Video Transcripts/Interviews\n",
    "\n",
    "There is a lot of spoke word out there. Podcasts, interviews, sales calls, phone calls.\n",
    "\n",
    "It is extremely useful to be able to extract this information and create value with it. We're going to run through an example focusing on the B2B sales use case.\n",
    "\n",
    "**Small plug:** This is a mini-preview on how I built [Thimble](https://thimbleai.com/) which helps Account Executives pull information from their sales calls. Sign up at https://thimbleai.com/ if you want to check it out or here's the [demo video](https://youtu.be/DIw4rbpI9ic) or see more information on [Twitter](https://twitter.com/GregKamradt)\n",
    "\n",
    "Plug over, Let's learn! 😈\n",
    "\n",
    "Through building Thimble I've learned a few tricks to make working with transcripts easier:\n",
    "1. **Name/Role -** Put the name of each person before their sentence. Bonus points if you have their role/company included too. Example: Greg (Marin Transitions): Hey! How's it going?\n",
    "2. **System instructions -** Be specific with your system prompt about the role you need you bot to play\n",
    "3. **Only pull from the call -** Emphasize not to make anything up\n",
    "4. **Don't make the user prompt -** Abstract away any user prompting necessary with key:value pairs\n",
    "\n",
    "First let's import our packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "37dedf37",
   "metadata": {
    "hide_input": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>.container { width:90% !important; }</style>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# To get environment variables\n",
    "import os\n",
    "\n",
    "# Make the display a bit wider\n",
    "from IPython.display import display, HTML\n",
    "display(HTML(\"<style>.container { width:90% !important; }</style>\"))\n",
    "\n",
    "# To split our transcript into pieces\n",
    "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
    "\n",
    "# Our chat model. We'll use the default which is gpt-3.5-turbo\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.chains.summarize import load_summarize_chain\n",
    "\n",
    "# Prompt templates for dynamic values\n",
    "from langchain.prompts.chat import (\n",
    "    ChatPromptTemplate,\n",
    "    SystemMessagePromptTemplate,\n",
    "    AIMessagePromptTemplate, # I included this one so you know you'll have it but we won't be using it\n",
    "    HumanMessagePromptTemplate\n",
    ")\n",
    "\n",
    "# To create our chat messages\n",
    "from langchain.schema import (\n",
    "    AIMessage,\n",
    "    HumanMessage,\n",
    "    SystemMessage\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9cae352a",
   "metadata": {
    "hide_input": false
   },
   "outputs": [],
   "source": [
    "os.environ['OPENAI_API_KEY'] = '...'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aacd8f98",
   "metadata": {},
   "source": [
    "Then let's load up our data. This is already a formatted transcript of a mock sales call. 70% of the hard part is getting clean data. Don't under estimate the reward you get for cleaning up your data first!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1f13dda0",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../data/Transcripts/acme_co_v2.txt', 'r') as file:\n",
    "    content = file.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "32b146c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Transcript:\n",
      "\n",
      "Greg (Marin Transitions Partner): Does this say the call is recording for you at all? \n",
      "\n",
      "Eliza (ACME Co): But it says the request, the report or in here? \n",
      "\n",
      "Greg (Marin Transitions Partner): Nice. All right… one sec. \n"
     ]
    }
   ],
   "source": [
    "print (\"Transcript:\\n\")\n",
    "print(content[:215]) # Why 215? Because it cut off at a clean line"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f6b6ceb",
   "metadata": {},
   "source": [
    "Split our documents so we don't run into token issues. Experiment with what chunk size words best for your use case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "042b7d95",
   "metadata": {},
   "outputs": [],
   "source": [
    "text_splitter = RecursiveCharacterTextSplitter(separators=[\"\\n\\n\", \"\\n\"], chunk_size=2000, chunk_overlap=250)\n",
    "texts = text_splitter.create_documents([content])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f22c76e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You have 5 texts\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Document(page_content=\"Greg (Marin Transitions Partner): Does this say the call is recording for you at all? \\n\\nEliza (ACME Co): But it says the request, the report or in here? \\n\\nGreg (Marin Transitions Partner): Nice. All right… one sec. \\n\\nGreg (Marin Transitions Partner): Cool. Absolutely. Well, I tell you what, thank you very much for joining me today. \\n\\nEliza (ACME Co): Yeah, glad to be chatting with you Greg. \\n\\nGreg (Marin Transitions Partner): Absolutely. Well. So, first of all, my name is Greg and I am from Marin transitions and we help people do social modeling on top of their businesses. So say somebody were to mention you on read it or mentioned you on Twitter or Facebook, then we help you understand what they're saying and the sentiment behind it. And I'm very excited to be chatting with you today. My first awesome. Well, I'll tell you what I've looked at your LinkedIn a little bit, but could you tell me more about your position and what you do? \\n\\nEliza (ACME Co): Absolutely. So I am cmo over at acmico and we are a software company that helps nonprofits grow their business. \\n\\nGreg (Marin Transitions Partner): Awesome. That's fabulous. And how many employees does acmico have? \\n\\nEliza (ACME Co): We have about 45 employees today. \\n\\nGreg (Marin Transitions Partner): Nice. That's great. And how big is the marketing team? \\n\\nEliza (ACME Co): The marketing team is, I think seven of us today. \\n\\nGreg (Marin Transitions Partner): Awesome. That's cool. And what technology stack does acme co use? \\n\\nEliza (ACME Co): So, today, we are using slack photoshop, Hubspot, Zoom and Google cloud. \\n\\nGreg (Marin Transitions Partner): That's awesome. And so, because we do social monitoring, I wanna go into a little bit of questions around your social experience, if you will, and what acne code does. So, how many times would you say that you post on social media per month? \\n\\nEliza (ACME Co): I'd say about 10 times a month?\", metadata={})"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print (f\"You have {len(texts)} texts\")\n",
    "texts[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d1415339",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your api key should be an environment variable, or else put it here\n",
    "# We are using a chat model in case you wanted to use gpt4\n",
    "llm = ChatOpenAI(temperature=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2950a62d",
   "metadata": {},
   "source": [
    "We're going to start with the vanilla load_summarize_chain to see how it goes.\n",
    "If you want to see the default prompts that are used you can explore the LangChain code. Here are the [map reduce prompts](https://github.com/hwchase17/langchain/blob/master/langchain/chains/summarize/map_reduce_prompt.py)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f57da26f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# verbose=True will output the prompts being sent to the \n",
    "chain = load_summarize_chain(llm, chain_type=\"map_reduce\", verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cde515c0",
   "metadata": {},
   "source": [
    "**Note:** At this point you might be asking, \"Greg, what about embeddings?\" Since we're just doing one call, I'm not worry about embeddings right now and putting the whole thing in a map reduce summarizer. If you have multiple calls and a lot of history then you should be embedding them for retrieval later. See tutorial on embeddings [here.](https://youtu.be/h0DHDp1FbmQ). [Tutorial on chain types.](https://youtu.be/f9_BWhCI4Zo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b1d2572f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new MapReduceDocumentsChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mWrite a concise summary of the following:\n",
      "\n",
      "\n",
      "\"Greg (Marin Transitions Partner): Does this say the call is recording for you at all? \n",
      "\n",
      "Eliza (ACME Co): But it says the request, the report or in here? \n",
      "\n",
      "Greg (Marin Transitions Partner): Nice. All right… one sec. \n",
      "\n",
      "Greg (Marin Transitions Partner): Cool. Absolutely. Well, I tell you what, thank you very much for joining me today. \n",
      "\n",
      "Eliza (ACME Co): Yeah, glad to be chatting with you Greg. \n",
      "\n",
      "Greg (Marin Transitions Partner): Absolutely. Well. So, first of all, my name is Greg and I am from Marin transitions and we help people do social modeling on top of their businesses. So say somebody were to mention you on read it or mentioned you on Twitter or Facebook, then we help you understand what they're saying and the sentiment behind it. And I'm very excited to be chatting with you today. My first awesome. Well, I'll tell you what I've looked at your LinkedIn a little bit, but could you tell me more about your position and what you do? \n",
      "\n",
      "Eliza (ACME Co): Absolutely. So I am cmo over at acmico and we are a software company that helps nonprofits grow their business. \n",
      "\n",
      "Greg (Marin Transitions Partner): Awesome. That's fabulous. And how many employees does acmico have? \n",
      "\n",
      "Eliza (ACME Co): We have about 45 employees today. \n",
      "\n",
      "Greg (Marin Transitions Partner): Nice. That's great. And how big is the marketing team? \n",
      "\n",
      "Eliza (ACME Co): The marketing team is, I think seven of us today. \n",
      "\n",
      "Greg (Marin Transitions Partner): Awesome. That's cool. And what technology stack does acme co use? \n",
      "\n",
      "Eliza (ACME Co): So, today, we are using slack photoshop, Hubspot, Zoom and Google cloud. \n",
      "\n",
      "Greg (Marin Transitions Partner): That's awesome. And so, because we do social monitoring, I wanna go into a little bit of questions around your social experience, if you will, and what acne code does. So, how many times would you say that you post on social media per month? \n",
      "\n",
      "Eliza (ACME Co): I'd say about 10 times a month?\"\n",
      "\n",
      "\n",
      "CONCISE SUMMARY:\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mWrite a concise summary of the following:\n",
      "\n",
      "\n",
      "\"Eliza (ACME Co): I'd say about 10 times a month? \n",
      "\n",
      "Greg (Marin Transitions Partner): Interesting. And do you do any sort of social monitoring today? Do you know what customers are saying about you on those platforms? \n",
      "\n",
      "Eliza (ACME Co): No, we have no idea. \n",
      "\n",
      "Greg (Marin Transitions Partner): Interesting. And what are the pain points you're having by not being able to do social monitoring? \n",
      "\n",
      "Eliza (ACME Co): Yeah, I'd say the pain points of not having social monitoring revolve around the lack of transparency. So we're by not understanding what our customers are saying about us on social media, that is a disadvantage in a very competitive market. \n",
      "\n",
      "Greg (Marin Transitions Partner): Yeah, absolutely. And would you or I guess, let me rephrase how valuable would it be to you to understand what customers are saying about your brand in real time? \n",
      "\n",
      "Eliza (ACME Co): I think it could have significant value to understand in real time. What's ap? \n",
      "\n",
      "Greg (Marin Transitions Partner): Happening. Nice. And if you were to put a dollar amount on that, what would you say? What would you say? The dollar amount would be? \n",
      "\n",
      "Eliza (ACME Co): I would estimate, you know, if each… having this kind of transparency and visibility into our market, if that enables us to close one more deal per month. Now, with, given our average contract values, I'd say 50,000 dollars. \n",
      "\n",
      "Greg (Marin Transitions Partner): Yeah, no, that's very cool. And who would be the person that would sign off on this purchase? \n",
      "\n",
      "Eliza (ACME Co): So there would be, that would be a committee decision between myself and the rest of the leadership team. But the ultimate signer of the contract would be my CEO Christina. \n",
      "\n",
      "Greg (Marin Transitions Partner): Interesting. And so as we think about this social monitoring even more, what are your priorities for a tool? What would you like to see?\"\n",
      "\n",
      "\n",
      "CONCISE SUMMARY:\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mWrite a concise summary of the following:\n",
      "\n",
      "\n",
      "\"Greg (Marin Transitions Partner): Interesting. And so as we think about this social monitoring even more, what are your priorities for a tool? What would you like to see? \n",
      "\n",
      "Eliza (ACME Co): I would like to see a tool that is able to accurately… in real time, tell me about… the data that my customers are saying about the company about our product… to stay ahead of, you know, potential risks. So protect that revenue and our existing customers. And then in terms of criteria for a partner, we were looking for someone that we can scale with as we build out our marketing program and someone that's going to offer the hands on white glove support that we need. \n",
      "\n",
      "Greg (Marin Transitions Partner): Yeah, no, that's awesome. And what is the process to get a new deal across the finish? Line? So, what is your evaluation process look like? \n",
      "\n",
      "Eliza (ACME Co): So, it is usually pretty easy process for us being a, an agile team over here. That process looks like I evaluate vendors, narrow that list down. We have about two options narrowed down today, one being your company. And then from there, I'll present and make my recommendations to the CEO. \n",
      "\n",
      "Greg (Marin Transitions Partner): Nice and say you make a decision, you're happy with it, what is the paper process that you need to go through to complete the decision? Is there accounting or legal or anybody? \n",
      "\n",
      "Eliza (ACME Co): Yes, there is a legal and security review as part of the process which would be conducted by our in house council. So you could send through your terms at any point and we could get ahead of that review if that would be… helpful. \n",
      "\n",
      "Greg (Marin Transitions Partner): Yeah, that's sweet. What are the, what other tools are you looking at as a part of this evaluation? \n",
      "\n",
      "Eliza (ACME Co): We're also looking at Hootsuite. \n",
      "\n",
      "Greg (Marin Transitions Partner): Interesting. Well, first, I wanna thank you very much for being such a big supporter of what we're doing here at Marin transitions?\"\n",
      "\n",
      "\n",
      "CONCISE SUMMARY:\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mWrite a concise summary of the following:\n",
      "\n",
      "\n",
      "\"Eliza (ACME Co): We're also looking at Hootsuite. \n",
      "\n",
      "Greg (Marin Transitions Partner): Interesting. Well, first, I wanna thank you very much for being such a big supporter of what we're doing here at Marin transitions? \n",
      "\n",
      "Eliza (ACME Co): Absolutely. I'm happy to be your champion over here. I think, you know, based on the great conversations that I've had… with your team in the past and my last company… and also your, the recommendations that I've heard in referrals from some of your other customers in my peers have gone a long way. \n",
      "\n",
      "Greg (Marin Transitions Partner): That's awesome. Do you have any questions for me? \n",
      "\n",
      "Eliza (ACME Co): Yeah. So as I mentioned, support is going to be important. I'm sure you're aware everyone is wanting to avoid shelfware these days. So what I'm curious about is how long does it take to get started on your platform? And what does that implementation process look? \n",
      "\n",
      "Greg (Marin Transitions Partner): Absolutely. So the implementation process is really in two parts. Part number one is just gonna be a about an hour long session with one of our implementation partners and your head of social media that's just to make sure that your accounts get set up and that we're doing the tracking properly. Then part two is going to be the actual monitoring part. It takes about four hours to start monitoring, but after that, it'll be available for you in real time and that's just so we can go scrape all the data off the social sites. So that net you should be up and running in about half a day's work. That sounds. \n",
      "\n",
      "Eliza (ACME Co): Perfect.\"\n",
      "\n",
      "\n",
      "CONCISE SUMMARY:\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mWrite a concise summary of the following:\n",
      "\n",
      "\n",
      "\"Eliza (ACME Co): Perfect. \n",
      "\n",
      "Greg (Marin Transitions Partner): Yeah, absolutely. Well, I tell you what, as a next step, what I'm gonna do is I'm going to share the brochure with you about our company that has all the information you may want on it. And I'm also going to share with you the pricing information as well. And so this is some deck that I'll send over. And so you can see that. And what I would love to do as a follow up is to schedule a new meeting with you for next week, probably on Wednesday. Is there a time that works on Wednesday for you? \n",
      "\n",
      "Eliza (ACME Co): Yes, I'm available any time between two and four PM on Wednesday, Pacific Time. So if you want that invite, I'll go ahead and accept it. \n",
      "\n",
      "Greg (Marin Transitions Partner): Cool. Absolutely. I'll send an invite for that time. \n",
      "\n",
      "Greg (Marin Transitions Partner): One last thing too. I always like to get to know my prospects a bit more on a personal level too. Do you have any pets or animals inside the house that are close to you? \n",
      "\n",
      "Eliza (ACME Co): I do. In fact, I have an Australian Shepherd dog. He's very cute and very crazy. \n",
      "\n",
      "Greg (Marin Transitions Partner): Awesome. No, that's great. And what do you like to do outside of? \n",
      "\n",
      "Eliza (ACME Co): Well, I live in the beautiful bay area in the north bay just north of San Francisco. So my husband and I really enjoy hiking on the nearby trails. We enjoy wine tasting up in wine country… and those are a couple of our favorite things to do on the weekends. \n",
      "\n",
      "Greg (Marin Transitions Partner): That's fabulous. Well, I tell you what, thank you very much for the conversation today and I'll follow up with you next week. \n",
      "\n",
      "Eliza (ACME Co): Great. I'm looking forward to. \n",
      "\n",
      "Greg (Marin Transitions Partner): It all right. We'll see you later. Bye bye.\"\n",
      "\n",
      "\n",
      "CONCISE SUMMARY:\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new StuffDocumentsChain chain...\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mWrite a concise summary of the following:\n",
      "\n",
      "\n",
      "\"Greg from Marin Transitions Partner speaks with Eliza from ACME Co about their business and social media presence. They discuss ACME Co's position as a software company that helps nonprofits grow their business, their number of employees and marketing team, and their technology stack. Greg also asks about their social media activity, to which Eliza responds that they post about 10 times a month.\n",
      "\n",
      "Eliza from ACME Co discusses with Greg from Marin Transitions Partner about their social monitoring practices and the pain points of not having transparency. Eliza estimates that having real-time visibility into their market could potentially close one more deal per month, which equates to $50,000. The decision to purchase a social monitoring tool would be a committee decision, with the CEO ultimately signing off on the contract. Greg asks about ACME Co's priorities for a social monitoring tool.\n",
      "\n",
      "Eliza from ACME Co is looking for a social monitoring tool that can accurately and in real-time tell her about customer feedback to protect revenue and existing customers. She is also looking for a partner that can scale with their marketing program and offer hands-on support. The evaluation process involves evaluating vendors, narrowing down options, and presenting recommendations to the CEO. There is a legal and security review process, and they are also considering Hootsuite as a tool. Marin Transitions Partner thanks Eliza for her support.\n",
      "\n",
      "Eliza from ACME Co is considering using Hootsuite and discusses her interest with Greg from Marin Transitions Partner. She asks about the implementation process and Greg explains that it involves an hour-long session and four hours of monitoring, with the platform being available in real-time afterwards. The process should take about half a day's work.\n",
      "\n",
      "Greg, a Marin Transitions Partner, plans to send a brochure and pricing information to Eliza from ACME Co. He schedules a follow-up meeting for next week and asks about her personal interests, including her Australian Shepherd dog and hiking in the bay area. They end the conversation with plans to speak again next week.\"\n",
      "\n",
      "\n",
      "CONCISE SUMMARY:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "output = chain.run(texts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2805165f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eliza from ACME Co discusses their business and social media presence with Greg from Marin Transitions Partner. They talk about ACME Co's position as a software company that helps nonprofits grow their business, their social media activity, and their need for a social monitoring tool. Eliza is looking for a tool that can accurately and in real-time tell her about customer feedback to protect revenue and existing customers. They discuss the implementation process for Hootsuite and schedule a follow-up meeting for next week.\n"
     ]
    }
   ],
   "source": [
    "print (output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6334e4f9",
   "metadata": {},
   "source": [
    "Not bad, but it's giving me the perspective of a 3rd party watching the conversation that is agnostic to the content. I want the AI bot to me on my side! I'll need to switch up the prompts to do this."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3758a0e0",
   "metadata": {},
   "source": [
    "### Custom Prompts\n",
    "\n",
    "I'm going to write custom prompts that give the AI more instructions on what role I want it to play"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b6933934",
   "metadata": {},
   "outputs": [],
   "source": [
    "template=\"\"\"\n",
    "\n",
    "You are a helpful assistant that helps {sales_rep_name}, a sales rep at {sales_rep_company}, summarize information from a sales call.\n",
    "Your goal is to write a summary from the perspective of {sales_rep_name} that will highlight key points that will be relevant to making a sale\n",
    "Do not respond with anything outside of the call transcript. If you don't know, say, \"I don't know\"\n",
    "Do not repeat {sales_rep_name}'s name in your output\n",
    "\n",
    "\"\"\"\n",
    "system_message_prompt = SystemMessagePromptTemplate.from_template(template)\n",
    "\n",
    "human_template=\"{text}\" # Simply just pass the text as a human message\n",
    "human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "05042a43",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat_prompt = ChatPromptTemplate.from_messages(messages=[system_message_prompt, human_message_prompt])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a3daced9",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = load_summarize_chain(llm,\n",
    "                             chain_type=\"map_reduce\",\n",
    "                             map_prompt=chat_prompt\n",
    "                            )\n",
    "\n",
    "# Because we aren't specifying a combine prompt the default one will be used"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d4ef0a5c",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "output = chain.run({\n",
    "                    \"input_documents\": texts,\n",
    "                    \"sales_rep_company\": \"Marin Transitions Partner\", \\\n",
    "                    \"sales_rep_name\" : \"Greg\"\n",
    "                   })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ca00b447",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ACME Co, a software company that helps nonprofits grow their business, is looking for a social monitoring tool to gain real-time visibility into their market and address their pain points. They have narrowed down their options to two vendors, including Marin Transitions Partner, and are interested in a partner that can scale with them as they build out their marketing program and offer hands-on white-glove support. Greg from Marin Transitions Partner spoke with Eliza, the CMO of ACME Co, and will be sending over a brochure and pricing information. They have scheduled a follow-up meeting for next Wednesday.\n"
     ]
    }
   ],
   "source": [
    "print (output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fda2fa3",
   "metadata": {},
   "source": [
    "Better! But say I wanted to change the format of the output without needing the user to do extra prompting."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff62d842",
   "metadata": {},
   "source": [
    "### Promptless changes\n",
    "\n",
    "To do this I'll write a few points about the different output types I would like. However, I'll that I'll expose to the user is a simple selection, radio button, or drop down. (We'll use text for now but you can do this in your app).\n",
    "\n",
    "I want to give the user the option to select between different summary output types.\n",
    "\n",
    "I'll have them pick between:\n",
    "1. One Sentence\n",
    "2. Bullet Points\n",
    "3. Short\n",
    "4. Long\n",
    "\n",
    "I could try to pass these words to the LLM, but I want to be more explicit with it. Plus, giving good instructions is the way to go!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5a4b52e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "summary_output_options = {\n",
    "    'one_sentence' : \"\"\"\n",
    "     - Only one sentence\n",
    "    \"\"\",\n",
    "    \n",
    "    'bullet_points': \"\"\"\n",
    "     - Bullet point format\n",
    "     - Separate each bullet point with a new line\n",
    "     - Each bullet point should be concise\n",
    "    \"\"\",\n",
    "    \n",
    "    'short' : \"\"\"\n",
    "     - A few short sentences\n",
    "     - Do not go longer than 4-5 sentences\n",
    "    \"\"\",\n",
    "    \n",
    "    'long' : \"\"\"\n",
    "     - A verbose summary\n",
    "     - You may do a few paragraphs to describe the transcript if needed\n",
    "    \"\"\"\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21b81dac",
   "metadata": {},
   "source": [
    "Create a new template that takes an additional parameter. I need to put this in the combined prompt so that the LLM will output in my format. If I did this in the map section I would lose the format after the combined prompt was done\n",
    "\n",
    "**Map Prompt**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "926ec377",
   "metadata": {},
   "outputs": [],
   "source": [
    "template=\"\"\"\n",
    "\n",
    "You are a helpful assistant that helps {sales_rep_name}, a sales rep at {sales_rep_company}, summarize information from a sales call.\n",
    "Your goal is to write a summary from the perspective of Greg that will highlight key points that will be relevant to making a sale\n",
    "Do not respond with anything outside of the call transcript. If you don't know, say, \"I don't know\"\n",
    "\"\"\"\n",
    "system_message_prompt_map = SystemMessagePromptTemplate.from_template(template)\n",
    "\n",
    "human_template=\"{text}\" # Simply just pass the text as a human message\n",
    "human_message_prompt_map = HumanMessagePromptTemplate.from_template(human_template)\n",
    "\n",
    "chat_prompt_map = ChatPromptTemplate.from_messages(messages=[system_message_prompt_map, human_message_prompt_map])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb7b8c0d",
   "metadata": {},
   "source": [
    "**Combined Prompt**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "178eb07d",
   "metadata": {},
   "outputs": [],
   "source": [
    "template=\"\"\"\n",
    "\n",
    "You are a helpful assistant that helps {sales_rep_name}, a sales rep at {sales_rep_company}, summarize information from a sales call.\n",
    "Your goal is to write a summary from the perspective of Greg that will highlight key points that will be relevant to making a sale\n",
    "Do not respond with anything outside of the call transcript. If you don't know, say, \"I don't know\"\n",
    "\n",
    "Respond with the following format\n",
    "{output_format}\n",
    "\n",
    "\"\"\"\n",
    "system_message_prompt_combine = SystemMessagePromptTemplate.from_template(template)\n",
    "\n",
    "human_template=\"{text}\" # Simply just pass the text as a human message\n",
    "human_message_prompt_combine = HumanMessagePromptTemplate.from_template(human_template)\n",
    "\n",
    "chat_prompt_combine = ChatPromptTemplate.from_messages(messages=[system_message_prompt_combine, human_message_prompt_combine])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8c3e9fc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = load_summarize_chain(llm,\n",
    "                             chain_type=\"map_reduce\",\n",
    "                             map_prompt=chat_prompt_map,\n",
    "                             combine_prompt=chat_prompt_combine,\n",
    "                             verbose=True\n",
    "                            )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "65e01887",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new MapReduceDocumentsChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mSystem: \n",
      "\n",
      "You are a helpful assistant that helps Greg, a sales rep at Marin Transitions Partner, summarize information from a sales call.\n",
      "Your goal is to write a summary from the perspective of Greg that will highlight key points that will be relevant to making a sale\n",
      "Do not respond with anything outside of the call transcript. If you don't know, say, \"I don't know\"\n",
      "\n",
      "Human: Greg (Marin Transitions Partner): Does this say the call is recording for you at all? \n",
      "\n",
      "Eliza (ACME Co): But it says the request, the report or in here? \n",
      "\n",
      "Greg (Marin Transitions Partner): Nice. All right… one sec. \n",
      "\n",
      "Greg (Marin Transitions Partner): Cool. Absolutely. Well, I tell you what, thank you very much for joining me today. \n",
      "\n",
      "Eliza (ACME Co): Yeah, glad to be chatting with you Greg. \n",
      "\n",
      "Greg (Marin Transitions Partner): Absolutely. Well. So, first of all, my name is Greg and I am from Marin transitions and we help people do social modeling on top of their businesses. So say somebody were to mention you on read it or mentioned you on Twitter or Facebook, then we help you understand what they're saying and the sentiment behind it. And I'm very excited to be chatting with you today. My first awesome. Well, I'll tell you what I've looked at your LinkedIn a little bit, but could you tell me more about your position and what you do? \n",
      "\n",
      "Eliza (ACME Co): Absolutely. So I am cmo over at acmico and we are a software company that helps nonprofits grow their business. \n",
      "\n",
      "Greg (Marin Transitions Partner): Awesome. That's fabulous. And how many employees does acmico have? \n",
      "\n",
      "Eliza (ACME Co): We have about 45 employees today. \n",
      "\n",
      "Greg (Marin Transitions Partner): Nice. That's great. And how big is the marketing team? \n",
      "\n",
      "Eliza (ACME Co): The marketing team is, I think seven of us today. \n",
      "\n",
      "Greg (Marin Transitions Partner): Awesome. That's cool. And what technology stack does acme co use? \n",
      "\n",
      "Eliza (ACME Co): So, today, we are using slack photoshop, Hubspot, Zoom and Google cloud. \n",
      "\n",
      "Greg (Marin Transitions Partner): That's awesome. And so, because we do social monitoring, I wanna go into a little bit of questions around your social experience, if you will, and what acne code does. So, how many times would you say that you post on social media per month? \n",
      "\n",
      "Eliza (ACME Co): I'd say about 10 times a month?\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mSystem: \n",
      "\n",
      "You are a helpful assistant that helps Greg, a sales rep at Marin Transitions Partner, summarize information from a sales call.\n",
      "Your goal is to write a summary from the perspective of Greg that will highlight key points that will be relevant to making a sale\n",
      "Do not respond with anything outside of the call transcript. If you don't know, say, \"I don't know\"\n",
      "\n",
      "Human: Eliza (ACME Co): I'd say about 10 times a month? \n",
      "\n",
      "Greg (Marin Transitions Partner): Interesting. And do you do any sort of social monitoring today? Do you know what customers are saying about you on those platforms? \n",
      "\n",
      "Eliza (ACME Co): No, we have no idea. \n",
      "\n",
      "Greg (Marin Transitions Partner): Interesting. And what are the pain points you're having by not being able to do social monitoring? \n",
      "\n",
      "Eliza (ACME Co): Yeah, I'd say the pain points of not having social monitoring revolve around the lack of transparency. So we're by not understanding what our customers are saying about us on social media, that is a disadvantage in a very competitive market. \n",
      "\n",
      "Greg (Marin Transitions Partner): Yeah, absolutely. And would you or I guess, let me rephrase how valuable would it be to you to understand what customers are saying about your brand in real time? \n",
      "\n",
      "Eliza (ACME Co): I think it could have significant value to understand in real time. What's ap? \n",
      "\n",
      "Greg (Marin Transitions Partner): Happening. Nice. And if you were to put a dollar amount on that, what would you say? What would you say? The dollar amount would be? \n",
      "\n",
      "Eliza (ACME Co): I would estimate, you know, if each… having this kind of transparency and visibility into our market, if that enables us to close one more deal per month. Now, with, given our average contract values, I'd say 50,000 dollars. \n",
      "\n",
      "Greg (Marin Transitions Partner): Yeah, no, that's very cool. And who would be the person that would sign off on this purchase? \n",
      "\n",
      "Eliza (ACME Co): So there would be, that would be a committee decision between myself and the rest of the leadership team. But the ultimate signer of the contract would be my CEO Christina. \n",
      "\n",
      "Greg (Marin Transitions Partner): Interesting. And so as we think about this social monitoring even more, what are your priorities for a tool? What would you like to see?\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mSystem: \n",
      "\n",
      "You are a helpful assistant that helps Greg, a sales rep at Marin Transitions Partner, summarize information from a sales call.\n",
      "Your goal is to write a summary from the perspective of Greg that will highlight key points that will be relevant to making a sale\n",
      "Do not respond with anything outside of the call transcript. If you don't know, say, \"I don't know\"\n",
      "\n",
      "Human: Greg (Marin Transitions Partner): Interesting. And so as we think about this social monitoring even more, what are your priorities for a tool? What would you like to see? \n",
      "\n",
      "Eliza (ACME Co): I would like to see a tool that is able to accurately… in real time, tell me about… the data that my customers are saying about the company about our product… to stay ahead of, you know, potential risks. So protect that revenue and our existing customers. And then in terms of criteria for a partner, we were looking for someone that we can scale with as we build out our marketing program and someone that's going to offer the hands on white glove support that we need. \n",
      "\n",
      "Greg (Marin Transitions Partner): Yeah, no, that's awesome. And what is the process to get a new deal across the finish? Line? So, what is your evaluation process look like? \n",
      "\n",
      "Eliza (ACME Co): So, it is usually pretty easy process for us being a, an agile team over here. That process looks like I evaluate vendors, narrow that list down. We have about two options narrowed down today, one being your company. And then from there, I'll present and make my recommendations to the CEO. \n",
      "\n",
      "Greg (Marin Transitions Partner): Nice and say you make a decision, you're happy with it, what is the paper process that you need to go through to complete the decision? Is there accounting or legal or anybody? \n",
      "\n",
      "Eliza (ACME Co): Yes, there is a legal and security review as part of the process which would be conducted by our in house council. So you could send through your terms at any point and we could get ahead of that review if that would be… helpful. \n",
      "\n",
      "Greg (Marin Transitions Partner): Yeah, that's sweet. What are the, what other tools are you looking at as a part of this evaluation? \n",
      "\n",
      "Eliza (ACME Co): We're also looking at Hootsuite. \n",
      "\n",
      "Greg (Marin Transitions Partner): Interesting. Well, first, I wanna thank you very much for being such a big supporter of what we're doing here at Marin transitions?\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mSystem: \n",
      "\n",
      "You are a helpful assistant that helps Greg, a sales rep at Marin Transitions Partner, summarize information from a sales call.\n",
      "Your goal is to write a summary from the perspective of Greg that will highlight key points that will be relevant to making a sale\n",
      "Do not respond with anything outside of the call transcript. If you don't know, say, \"I don't know\"\n",
      "\n",
      "Human: Eliza (ACME Co): We're also looking at Hootsuite. \n",
      "\n",
      "Greg (Marin Transitions Partner): Interesting. Well, first, I wanna thank you very much for being such a big supporter of what we're doing here at Marin transitions? \n",
      "\n",
      "Eliza (ACME Co): Absolutely. I'm happy to be your champion over here. I think, you know, based on the great conversations that I've had… with your team in the past and my last company… and also your, the recommendations that I've heard in referrals from some of your other customers in my peers have gone a long way. \n",
      "\n",
      "Greg (Marin Transitions Partner): That's awesome. Do you have any questions for me? \n",
      "\n",
      "Eliza (ACME Co): Yeah. So as I mentioned, support is going to be important. I'm sure you're aware everyone is wanting to avoid shelfware these days. So what I'm curious about is how long does it take to get started on your platform? And what does that implementation process look? \n",
      "\n",
      "Greg (Marin Transitions Partner): Absolutely. So the implementation process is really in two parts. Part number one is just gonna be a about an hour long session with one of our implementation partners and your head of social media that's just to make sure that your accounts get set up and that we're doing the tracking properly. Then part two is going to be the actual monitoring part. It takes about four hours to start monitoring, but after that, it'll be available for you in real time and that's just so we can go scrape all the data off the social sites. So that net you should be up and running in about half a day's work. That sounds. \n",
      "\n",
      "Eliza (ACME Co): Perfect.\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mSystem: \n",
      "\n",
      "You are a helpful assistant that helps Greg, a sales rep at Marin Transitions Partner, summarize information from a sales call.\n",
      "Your goal is to write a summary from the perspective of Greg that will highlight key points that will be relevant to making a sale\n",
      "Do not respond with anything outside of the call transcript. If you don't know, say, \"I don't know\"\n",
      "\n",
      "Human: Eliza (ACME Co): Perfect. \n",
      "\n",
      "Greg (Marin Transitions Partner): Yeah, absolutely. Well, I tell you what, as a next step, what I'm gonna do is I'm going to share the brochure with you about our company that has all the information you may want on it. And I'm also going to share with you the pricing information as well. And so this is some deck that I'll send over. And so you can see that. And what I would love to do as a follow up is to schedule a new meeting with you for next week, probably on Wednesday. Is there a time that works on Wednesday for you? \n",
      "\n",
      "Eliza (ACME Co): Yes, I'm available any time between two and four PM on Wednesday, Pacific Time. So if you want that invite, I'll go ahead and accept it. \n",
      "\n",
      "Greg (Marin Transitions Partner): Cool. Absolutely. I'll send an invite for that time. \n",
      "\n",
      "Greg (Marin Transitions Partner): One last thing too. I always like to get to know my prospects a bit more on a personal level too. Do you have any pets or animals inside the house that are close to you? \n",
      "\n",
      "Eliza (ACME Co): I do. In fact, I have an Australian Shepherd dog. He's very cute and very crazy. \n",
      "\n",
      "Greg (Marin Transitions Partner): Awesome. No, that's great. And what do you like to do outside of? \n",
      "\n",
      "Eliza (ACME Co): Well, I live in the beautiful bay area in the north bay just north of San Francisco. So my husband and I really enjoy hiking on the nearby trails. We enjoy wine tasting up in wine country… and those are a couple of our favorite things to do on the weekends. \n",
      "\n",
      "Greg (Marin Transitions Partner): That's fabulous. Well, I tell you what, thank you very much for the conversation today and I'll follow up with you next week. \n",
      "\n",
      "Eliza (ACME Co): Great. I'm looking forward to. \n",
      "\n",
      "Greg (Marin Transitions Partner): It all right. We'll see you later. Bye bye.\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new StuffDocumentsChain chain...\u001b[0m\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mSystem: \n",
      "\n",
      "You are a helpful assistant that helps Greg, a sales rep at Marin Transitions Partner, summarize information from a sales call.\n",
      "Your goal is to write a summary from the perspective of Greg that will highlight key points that will be relevant to making a sale\n",
      "Do not respond with anything outside of the call transcript. If you don't know, say, \"I don't know\"\n",
      "\n",
      "Respond with the following format\n",
      "\n",
      "     - Only one sentence\n",
      "    \n",
      "\n",
      "\n",
      "Human: Greg from Marin Transitions Partner spoke with Eliza, the CMO of ACME Co, about their business and social media presence. ACME Co is a software company that helps nonprofits grow their business with 45 employees and a marketing team of seven. They use Slack, Photoshop, Hubspot, Zoom, and Google Cloud. Eliza estimates that they post about 10 times a month on social media. Marin Transitions Partner helps businesses with social modeling to understand sentiment behind mentions on social media.\n",
      "\n",
      "Greg from Marin Transitions Partner spoke with Eliza from ACME Co about their social monitoring practices. Eliza stated that they have no idea what customers are saying about them on social media, which is a disadvantage in a competitive market. Greg highlighted the value of understanding what customers are saying about their brand in real-time and asked Eliza to put a dollar amount on it. Eliza estimated that having this kind of transparency and visibility into their market could enable them to close one more deal per month, which would be worth $50,000. The decision to purchase would be made by a committee, with the ultimate signer being the CEO Christina. Greg asked about their priorities for a social monitoring tool.\n",
      "\n",
      "Eliza from ACME Co is interested in a social monitoring tool that can accurately and in real-time tell her about the data that her customers are saying about the company and its products to stay ahead of potential risks. She is also looking for a partner that can scale with them as they build out their marketing program and offer hands-on white-glove support. The evaluation process involves Eliza evaluating vendors, narrowing down the list to two options, and presenting her recommendations to the CEO. There is a legal and security review as part of the process, which would be conducted by their in-house counsel. ACME Co is also evaluating Hootsuite as another option.\n",
      "\n",
      "Eliza from ACME Co is interested in Hootsuite, but is a big supporter of Marin Transitions Partner. She is concerned about support and avoiding shelfware. She asks about the implementation process and how long it takes to get started on the platform. Greg explains that the implementation process is in two parts, with an hour-long session with an implementation partner and the head of social media to set up accounts and ensure proper tracking. The second part is the monitoring process, which takes about four hours to start, but after that, it will be available in real-time. Overall, the implementation process takes about half a day's work.\n",
      "\n",
      "Greg from Marin Transitions Partner had a successful sales call with Eliza from ACME Co. He will be sending over a brochure and pricing information for their company. They scheduled a follow-up meeting for next Wednesday between 2-4 PM Pacific Time. Greg also asked Eliza about her personal life and found out that she has an Australian Shepherd dog and enjoys hiking and wine tasting in the nearby trails and wine country.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "user_selection = 'one_sentence'\n",
    "\n",
    "output = chain.run({\n",
    "                    \"input_documents\": texts,\n",
    "                    \"sales_rep_company\": \"Marin Transitions Partner\", \\\n",
    "                    \"sales_rep_name\" : \"Greg\",\n",
    "                    \"output_format\" : summary_output_options[user_selection]\n",
    "                   })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "65e65c2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ACME Co is interested in a social monitoring tool that can accurately and in real-time tell her about the data that her customers are saying about the company and its products to stay ahead of potential risks, and is evaluating Marin Transitions Partner and Hootsuite as options.\n"
     ]
    }
   ],
   "source": [
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04e44a1e",
   "metadata": {},
   "source": [
    "Awesome! Now we have a bullet point format without needing to have the user specify any additional information.\n",
    "\n",
    "If you wanted to productionize this you would need to add additional prompts to extract other information from the calls that may be helpful to a sales person. Example: Key Points + Next Steps from the call. You should also parallelize the map calls if you do the map reduce method.\n",
    "\n",
    "Have other ideas about how something like this could be used? Send me a tweet or DM on [Twitter](https://twitter.com/GregKamradt) or contact@dataindependent.com"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
